package com.swotplus.framework.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.swotplus.common.utils.spring.SpringUtils;
import com.swotplus.system.sys.entity.SysDictData;
import com.swotplus.system.sys.service.SysDictDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
public class DictMap {

    private static final Logger logger = LoggerFactory.getLogger("DictMap");
    private  static SysDictDataService sysDictDataService = SpringUtils.getBean(SysDictDataService.class);

    static {
        sysDictDataService = SpringUtils.getBean(SysDictDataService.class);

    }

    public static Multimap<String, String[]> dictMap = null;


    /**
     * 初始化字典
     */

    private void initDict() {
        logger.info("开始数据字典初始化");
        // 1. 读取数据库全部字典信息
        List<SysDictData> list = sysDictDataService.list();
        if (list != null && list.size() > 0) {
            if (dictMap != null) {
                dictMap.clear();
            } else {
                dictMap = ArrayListMultimap.create();
            }
            for (SysDictData d : list) {
                dictMap.put(d.getDictType(), new String[]{d.getDictValue(), d.getDictLabel()});
            }
        }
        logger.info("结束数据字典初始化");
    }

    @PostConstruct
    private void inist() {
        initDict();
    }
}
